<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Connect Server Test</title>
    <script src="/packages/libs/ofa/ofa.js" type="module"></script>
  </head>
  <body>
    <script type="module">
      import { test } from "/ok-test/main.js";
      import { createUser } from "/packages/user/main.js";

      await test("Server Connection Test", async () => {
        // 创建用户
        const user = await createUser({ user: "test-connection-user" });
        const serverUrl = "ws://localhost:18290";

        // 连接服务器
        const serverClient = await user.connectServer(serverUrl);

        // 检查连接是否成功
        const hasServerClient = !!serverClient;
        const hasSendToMethod = typeof serverClient.sendTo === "function";
        const hasIsUserOnlineMethod =
          typeof serverClient.isUserOnline === "function";

        return {
          assert: hasServerClient && hasSendToMethod && hasIsUserOnlineMethod,
          content: {
            message: "服务器连接成功",
            hasServerClient,
            hasSendToMethod,
            hasIsUserOnlineMethod,
          },
        };
      });

      await test("User Online Status Test", async () => {
        // 创建两个用户
        const user1 = await createUser({ user: "test-user1" });
        const user2 = await createUser({ user: "test-user2" });

        const serverUrl = "ws://localhost:18290";

        // 两者都连上同一个服务器
        const serverClient1 = await user1.connectServer(serverUrl);
        const serverClient2 = await user2.connectServer(serverUrl);

        // 等待连接建立
        await new Promise((resolve) => setTimeout(resolve, 300));

        // 查找对方是否在线
        const isUser2Online = await serverClient1.isUserOnline(user2.userId);
        const isUser1Online = await serverClient2.isUserOnline(user1.userId);

        return {
          assert: isUser2Online && isUser1Online,
          content: {
            message: "用户在线状态检测成功",
            user1Id: user1.userId,
            user2Id: user2.userId,
            isUser2Online,
            isUser1Online,
          },
        };
      });

      await test("Data Transmission Test", async () => {
        // 创建两个用户
        const user1 = await createUser({ user: "test-sender" });
        const user2 = await createUser({ user: "test-receiver" });

        const serverUrl = "ws://localhost:18290";

        // 两者都连上同一个服务器
        const serverClient1 = await user1.connectServer(serverUrl);
        const serverClient2 = await user2.connectServer(serverUrl);

        // 设置接收数据的Promise
        const receiveDataPromise = new Promise((resolve) => {
          serverClient2.onData = (fromUserId, data) => {
            resolve({ fromUserId, data });
          };
        });

        // 等待连接建立
        await new Promise((resolve) => setTimeout(resolve, 300));

        // 发送数据给对方
        const testData = {
          val: "hello test-receiver",
          timestamp: Date.now(),
        };

        serverClient1.sendTo({ userId: user2.userId }, testData);

        // 等待接收数据
        const received = await receiveDataPromise;
        const isCorrectSender = received.fromUserId === user1.userId;
        const isCorrectData =
          JSON.stringify(received.data) === JSON.stringify(testData);

        return {
          assert: isCorrectSender && isCorrectData,
          content: {
            message: "数据传输测试成功",
            sentData: testData,
            receivedData: received.data,
            fromUserId: received.fromUserId,
            isCorrectSender,
            isCorrectData,
          },
        };
      });

      await test("Uint8Array Transmission Test", async () => {
        // 创建两个用户
        const user1 = await createUser({ user: "test-sender2" });
        const user2 = await createUser({ user: "test-receiver2" });

        const serverUrl = "ws://localhost:18290";

        // 两者都连上同一个服务器
        const serverClient1 = await user1.connectServer(serverUrl);
        const serverClient2 = await user2.connectServer(serverUrl);

        // 等待连接建立
        await new Promise((resolve) => setTimeout(resolve, 300));

        // 创建测试用的 Uint8Array 数据
        const testData = new Uint8Array([1, 2, 3, 4, 5]);

        // 设置接收数据的Promise
        const receiveDataPromise = new Promise((resolve) => {
          serverClient2.onData = (fromUserId, data) => {
            resolve({ fromUserId, data });
          };
        });

        // 发送 Uint8Array 数据给对方
        serverClient1.sendTo({ userId: user2.userId }, testData);

        // 等待接收数据
        const received = await receiveDataPromise;
        const isCorrectSender = received.fromUserId === user1.userId;
        const isCorrectData =
          received.data.length === testData.length &&
          received.data.every((value, index) => value === testData[index]);

        return {
          assert: isCorrectSender && isCorrectData,
          content: {
            message: "Uint8Array 数据传输测试成功",
            sentData: Array.from(testData),
            receivedData: Array.from(received.data),
            fromUserId: received.fromUserId,
            isCorrectSender,
            isCorrectData,
          },
        };
      });

      await test("Multiple Users Independent Connections Test", async () => {
        // 创建多个用户
        const user1 = await createUser({ user: "test-multi-user1" });
        const user2 = await createUser({ user: "test-multi-user2" });
        const user3 = await createUser({ user: "test-multi-user3" });

        const serverUrl = "ws://localhost:18290";

        // 所有用户连接到同一个服务器
        const serverClient1 = await user1.connectServer(serverUrl);
        const serverClient2 = await user2.connectServer(serverUrl);
        const serverClient3 = await user3.connectServer(serverUrl);

        // 等待连接建立
        await new Promise((resolve) => setTimeout(resolve, 300));

        // 检查所有用户都在线
        const isUser2Online = await serverClient1.isUserOnline(user2.userId);
        const isUser3Online = await serverClient1.isUserOnline(user3.userId);
        const isUser1OnlineFrom2 = await serverClient2.isUserOnline(
          user1.userId
        );
        const isUser3OnlineFrom2 = await serverClient2.isUserOnline(
          user3.userId
        );

        return {
          assert:
            isUser2Online &&
            isUser3Online &&
            isUser1OnlineFrom2 &&
            isUser3OnlineFrom2,
          content: {
            message: "多用户独立连接测试成功",
            user1Id: user1.userId,
            user2Id: user2.userId,
            user3Id: user3.userId,
            isUser2Online,
            isUser3Online,
            isUser1OnlineFrom2,
            isUser3OnlineFrom2,
          },
        };
      });

      await test("Direct User Connection Test", async () => {
        // 创建两个用户
        const user1 = await createUser({ user: "test-direct-user1" });
        const user2 = await createUser({ user: "test-direct-user2" });

        const serverUrl = "ws://localhost:18290";

        // 保证user2连上服务器
        // 原代码使用 user2.servers() 获取服务器列表，这里应该直接使用已知的 serverUrl
        await user2.connectServer(serverUrl);

        // 设置接收数据的Promise
        const receiveDataPromise = new Promise((resolve) => {
          user2.addEventListener("receive-data", (e) => {
            const { fromUserId, fromUserSessionId, data, server } = e.detail;

            resolve({ fromUserId, fromUserSessionId, data, server });
          });
        });

        // 保证user1也连上服务器，因为直接用户连接可能需要双方都连接到服务器
        await user1.connectServer(serverUrl);

        // 直接连接 user2
        let remoteUser2;
        try {
          remoteUser2 = await user1.connectUser({ userId: user2.userId });
        } catch (error) {
          console.log("连接用户失败:", error);
          throw error;
        }

        // 发送数据给对方
        const testData = {
          val: "hello user2",
          timestamp: Date.now(),
        };

        // 通过用户1生成的远端用户2实例发送数据到用户2上
        remoteUser2.post(testData);

        // 等待接收数据
        const received = await receiveDataPromise;
        const isCorrectSender = received.fromUserId === user1.userId;
        const isCorrectSession = received.fromUserSessionId === user1.sessionId;
        const isCorrectData =
          JSON.stringify(received.data) === JSON.stringify(testData);

        return {
          assert: isCorrectSender && isCorrectSession && isCorrectData,
          content: {
            message: "直接用户连接测试成功",
            sentData: testData,
            receivedData: received.data,
            fromUserId: received.fromUserId,
            fromUserSessionId: received.fromUserSessionId,
            isCorrectSender,
            isCorrectSession,
            isCorrectData,
          },
        };
      });
    </script>
  </body>
</html>
